package cn.itcast.erp.action;

import cn.itcast.erp.biz.IDepBiz;
import cn.itcast.erp.entity.Dep;
import cn.itcast.erp.utils.AjaxReturnUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 采取属性驱动的方式来获取前台传递的参数，action中所有方法都没有参数
 */
public class DepAction {

    //使用配置文件方式注入，先在spring里面配置一个bean，然后获取set方法即可
    private IDepBiz depBiz;

    public void setDepBiz(IDepBiz depBiz) {
        this.depBiz = depBiz;
    }

    //采取属性驱动的方式来获取前台传递的查询条件
    private Dep dep;
    private Dep dep2;
    private Object param;

    public Dep getDep2() {
        return dep2;
    }
    public void setDep2(Dep dep2) {
        this.dep2 = dep2;
    }
    public Object getParam() {
        return param;
    }
    public void setParam(Object param) {
        this.param = param;
    }
    public Dep getDep() {
        return dep;
    }
    public void setDep(Dep dep) {
        this.dep = dep;
    }

    //页码
    private int page;
    //每页记录数
    private int rows;

    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getRows() {
        return rows;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }

    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }




    /**
     * 扩展多条件查询dep
     */
    public void getListByCondition() throws IOException {
        List<Dep> depList = depBiz.getListByCondition(dep, dep2, param);
        String jsonString = JSON.toJSONString(depList);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().print(jsonString);
    }


    /**
     * 查询总数据条数
     */
    public long getCount(){
        long count = depBiz.getCount(dep, dep2, param);
        return count;
    }


    /**
     * 分页查询
     */
    public void listByPage() throws IOException {
        //从第几条开始
        int firstResult = (page - 1) * rows;
        List<Dep> depList = depBiz.listByPage(dep, dep2, param, firstResult, rows);

        long count = depBiz.getCount(dep, dep2, param);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", depList);
        map.put("total", count);
        String jsonString = JSON.toJSONString(map);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().print(jsonString);
    }


    /**
     * 添加部门
     */
    public void add(){
        dep.setUuid(UUID.randomUUID().toString());
        depBiz.add(dep);
        AjaxReturnUtil.JsonReturn(true, "添加成功");
    }


    /**
     * 删除部门
     */
    public void delete(){
        depBiz.delete(id);
        AjaxReturnUtil.JsonReturn(true, "删除成功");
    }


    /**
     * 查询一个
     */
    public void get() throws IOException {
        Dep dep3 = depBiz.get(id);
        String jsonString = JSON.toJSONString(dep3);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        String json = mapJson(jsonString, "dep");
        writer.write(json);
    }


    /**
     * 修改部门
     */
    public  void update(){
        depBiz.update(dep);
        AjaxReturnUtil.JsonReturn(true, "修改成功");
    }


    private String mapJson(String jsonString, String prefix){
        Map<String, Object> map = JSON.parseObject(jsonString);
        HashMap<String, Object> newMap = new HashMap<String, Object>();
        for (String key : map.keySet()) {
            newMap.put(prefix + "." + key , map.get(key));
        }
        return JSON.toJSONString(newMap);
    }

}
